<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/koala.svg" alt="logo" />
      <span v-if="!collapse" class="title">vue3+ts</span>
    </div>

    <el-menu :default-active="currentMenuId" class="el-menu-vertical" background-color="#0c2135" :collapse="collapse" text-color="#b7bdc3" active-text-color="#0a60bd">
      <template v-for="item in userMenus" :key="item.id">
        <template v-if="item.type === 1">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <i v-if="item.icon" :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>

            <template v-for="subItem in item.children" :key="subItem.id">
              <el-menu-item :index="subItem.id + ''" @click="handleMenuItemClick(subItem)">
                <i v-if="item.icon" :class="item.icon"></i>
                <span>{{ subItem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
          <!-- 1 表示可以展开的菜单 -->
        </template>

        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''" @click="handleMenuItemClick(item)">
            <i v-if="item.icon" :class="item.icon"></i>
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { ElMenu, ElSubMenu, ElMenuItemGroup, ElMenuItem } from 'element-plus'
import { useStore } from '@/store'
import { useRouter, useRoute } from 'vue-router'
import { getMenu8Route } from '@/utils/map-menus'

export default defineComponent({
  components: { ElMenu, ElSubMenu, ElMenuItemGroup, ElMenuItem },
  props: {
    collapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const store = useStore()
    // const userMenus = computed(() => {
    //   // 请求来的用户拥有的菜单项是有 root 节点的
    //   // 要将该 root 节点去掉后的数组作为构建左侧菜单的数据
    //   const withRootMenus = store.state.login.userMenus
    //   const menus: IMenuItem = withRootMenus[0].children

    //   return menus
    // })

    const userMenus = computed(() => store.state.login.userMenus)
    const router = useRouter()
    const handleMenuItemClick = (item: any) => {
      console.log(item)
      router.push({
        path: item.url ?? '/not-found' // 菜单项对应的 url 在路由表中不存在则跳转到 404 页面
      })
    }

    // 根据当前路由选中左侧菜单项
    const route = useRoute()
    const currentPath = route.path
    console.log('根据路由查询获取对应菜单项的两个参数，userMenus:', userMenus.value, 'currentPath:', currentPath)
    const currentMenu = getMenu8Route(userMenus.value, currentPath)
    console.log('currentMenu:', currentMenu)
    const currentMenuId = ref(currentMenu.id + '')
    // const currentMenuId = ref(currentMenu.id + '')
    // console.log('当前路由对应的菜单项是：', currentMenu, 'currentMenuId:', currentMenuId)

    return { userMenus, handleMenuItemClick, currentMenuId }
  }
})
</script>
<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }
}
</style>
